{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data_file = '/Users/sarabeery/Documents/CameraTrapClass/DataWrangling/Databases/ECCV.json'\n",
    "with open(data_file, 'r') as f:\n",
    "     data = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "tax_file = '/Users/sarabeery/Documents/CameraTrapClass/DataWrangling/CreatingDatabases/category_data_with_taxonomy.json'\n",
    "with open(tax_file, 'r') as f:\n",
    "     taxonomy = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "db_file = '/Users/sarabeery/Documents/CameraTrapClass/DataWrangling/CreatingDatabases/inat_category_lookup.p'\n",
    "inat_cat_lookup = pickle.load(open(db_file,'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[u'car', 33]\n",
      "[u'empty', 30]\n"
     ]
    }
   ],
   "source": [
    "images = data['images']\n",
    "categories = data['categories']\n",
    "annotations = data['annotations']\n",
    "cat_names = [[cat['name'],cat['id']] for cat in categories]\n",
    "for cat in cat_names:\n",
    "    if cat[0] not in inat_cat_lookup:\n",
    "        print(cat)\n",
    "cat_to_id = {}\n",
    "id_to_cat = {}\n",
    "for cat in categories:\n",
    "    cat_to_id[cat['name']] = cat['id']\n",
    "    id_to_cat[cat['id']] = cat['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5089\n",
      "{u'supercategory': u'Insecta', u'ancestors': [{u'name': u'Animalia', u'rank': u'kingdom'}, {u'name': u'Arthropoda', u'rank': u'phylum'}, {u'name': u'Hexapoda', u'rank': u'subphylum'}, {u'name': u'Insecta', u'rank': u'class'}, {u'name': u'Pterygota', u'rank': u'subclass'}, {u'name': u'Coleoptera', u'rank': u'order'}, {u'name': u'Polyphaga', u'rank': u'suborder'}, {u'name': u'Staphyliniformia', u'rank': u'suborder'}, {u'name': u'Staphylinoidea', u'rank': u'superfamily'}, {u'name': u'Silphidae', u'rank': u'family'}, {u'name': u'Nicrophorinae', u'rank': u'subfamily'}, {u'name': u'Nicrophorus', u'rank': u'genus'}], u'id': 1916, u'name': u'Nicrophorus tomentosus'}\n"
     ]
    }
   ],
   "source": [
    "print(len(taxonomy))\n",
    "print(taxonomy[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{u'supercategory': u'Mammalia', u'ancestors': [{u'name': u'Animalia', u'rank': u'kingdom'}, {u'name': u'Chordata', u'rank': u'phylum'}, {u'name': u'Vertebrata', u'rank': u'subphylum'}, {u'name': u'Mammalia', u'rank': u'class'}, {u'name': u'Rodentia', u'rank': u'order'}, {u'name': u'Sciuridae', u'rank': u'family'}, {u'name': u'Xerinae', u'rank': u'subfamily'}, {u'name': u'Marmotini', u'rank': u'tribe'}, {u'name': u'Tamias', u'rank': u'genus'}], u'id': 230, u'name': u'Tamias dorsalis'}\n"
     ]
    }
   ],
   "source": [
    "for taxa in taxonomy:\n",
    "    if taxa['name'] == 'Tamias dorsalis':\n",
    "        print(taxa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(u'opossum', 'Didelphis'), (u'deer', 'Cervidae'), (u'raccoon', 'Procyon lotor'), (u'squirrel', 'Sciuridae'), (u'bobcat', 'Lynx rufus'), (u'skunk', 'Mephitidae'), (u'dog', 'Canis familiaris'), (u'coyote', 'Canis latrans'), (u'rabbit', 'Leporidae'), (u'bird', 'Aves'), (u'rodent', 'Muridae'), (u'cat', 'Felis catus'), (u'fox', 'Vulpes'), (u'badger', 'Mustelidae')]\n"
     ]
    }
   ],
   "source": [
    "print([(cat['name'],inat_cat_lookup[cat['name']]) for cat in categories if cat['name'] in inat_cat_lookup])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "map_inat_to_eccv = {}\n",
    "for taxa in taxonomy:\n",
    "    #raccoon\n",
    "    if taxa['name'] == 'Procyon lotor':\n",
    "        map_inat_to_eccv[taxa['id']] = cat_to_id['raccoon']\n",
    "    #bobcat\n",
    "    elif taxa['name'] == 'Lynx rufus':\n",
    "        map_inat_to_eccv[taxa['id']] = cat_to_id['bobcat']\n",
    "    #coyote\n",
    "    elif taxa['name'] == 'Canis latrans':\n",
    "        map_inat_to_eccv[taxa['id']] = cat_to_id['coyote']\n",
    "    #cat\n",
    "    elif taxa['name'] == 'Felis catus':\n",
    "        map_inat_to_eccv[taxa['id']] = cat_to_id['cat']\n",
    "    #dog\n",
    "    elif taxa['name'] == 'Canis familiaris':\n",
    "        map_inat_to_eccv[taxa['id']] = cat_to_id['dog']\n",
    "    else: #no direct species match\n",
    "        for a in taxa['ancestors']:\n",
    "            #opossum\n",
    "            if a['rank'] == 'genus' and a['name'] == 'Didelphis':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['opossum']\n",
    "            #fox\n",
    "            elif a['rank'] == 'genus' and a['name'] == 'Vulpes':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['fox']\n",
    "            #badger\n",
    "            elif a['rank'] == 'family' and a['name'] == 'Mustelidae':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['badger']\n",
    "            #deer\n",
    "            elif a['rank'] == 'family' and a['name'] == 'Cervidae':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['deer']  \n",
    "            #squirrel\n",
    "            elif a['rank'] == 'family' and a['name'] == 'Sciuridae':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['squirrel']\n",
    "            #skunk\n",
    "            elif a['rank'] == 'family' and a['name'] == 'Mephitidae':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['skunk']   \n",
    "            #rabbit\n",
    "            elif a['rank'] == 'family' and a['name'] == 'Leporidae':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['rabbit']\n",
    "            #rodent\n",
    "            elif a['rank'] == 'family' and a['name'] == 'Muridae':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['rodent']  \n",
    "            #bird\n",
    "            elif a['rank'] == 'class' and a['name'] == 'Aves':\n",
    "                map_inat_to_eccv[taxa['id']] = cat_to_id['bird']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pickle.dump(map_inat_to_eccv,open('map_inat_to_eccv.p','wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1042\n"
     ]
    }
   ],
   "source": [
    "print(len(map_inat_to_eccv))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "u'squirrel'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id_to_cat[map_inat_to_eccv[230]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
